package com.zking.controller;

import java.sql.Date;
import java.util.List;
import java.util.Map;

import javax.jms.Session;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zking.model.Collects;
import com.zking.model.Questions;
import com.zking.model.Users;
import com.zking.service.QuestionService;
import com.zking.service.UserService;
import com.zking.util.AppException;
import com.zking.util.MD5;
import com.zking.util.Result;


@Controller
@RequestMapping(value="/login")
public class UserController {
	@Autowired
	@Qualifier("userService")
	private UserService userService;
	@Autowired
	@Qualifier("questionService")
	private QuestionService questionService;
	//登陆或注册成功
	@RequestMapping("/index")
		public String index(){
				return "index";
			}
	 
	
	
	//注册
	@RequestMapping(value="/reg",produces = "application/json;charset=utf-8")
		public @ResponseBody String reg(HttpServletResponse response,String email,String pwd,String phone){
			String str="";
			//密码加密
			MD5 md5 = new MD5();
			Users user= new Users();
			//获取用户名和密码
			user.setuEmail(email);
			user.setuPwd(md5.DoMD5(pwd));
			user.setuPhone(phone);
			user.setuName(email);
			try {
				userService.Register(user);
				str="1";
			} catch (AppException e) {
				str="2";
			}
			return str;
	}
	
	
	//进入提问页面
	@RequestMapping(value="/askquestion")
	public @ResponseBody String askquestion(HttpServletRequest req){
		Users user = (Users) req.getSession().getAttribute("user");
		String str = "";
		if(null==user){
			str = "0";
		}else{
			str = "1";
		}
		
		return str;
	}
	
	//判断用户是否已被注册
	@RequestMapping(value="/validate",produces = "application/json;charset=utf-8")
	public @ResponseBody String validate(String email){
		String str = "";
		try {
			if(userService.checkEmail(email)==0){
				str="0";
			}else {
				str="1";
			}
		} catch (AppException e) {
			str= e.toString();
		}
		return str;
	}
	//判断用户是否存在
	@RequestMapping(value="/login",produces = "application/json;charset=utf-8")
	public @ResponseBody String validate2(HttpServletRequest req, String email,String pwd){
		String str = "";
		HttpSession session = req.getSession();
		try {
			if(null!=userService.validateLogin(email, pwd)){
				//将用户信息存入session
				session.setAttribute("user", userService.validateLogin(email, pwd));
				str="0";
			}else {
				//System.out.println(1);
				str="1";
			}
		} catch (AppException e) {
			str= e.toString();
		}
		return str;
	}

	//登录用户查看个人信息
	@RequestMapping(value="/myinfo/{uId}",method = RequestMethod.GET)
	public String userinfo(HttpServletRequest req,@PathVariable Integer uId,Model model){
		Users user = (Users) req.getSession().getAttribute("user");
		List<Questions> qlist = null;
		List<Collects> clist = null;
		HttpSession session = req.getSession();
		try {
			session.setAttribute("user", userService.selectUsersByuid(uId));
			qlist = questionService.selectByuid(uId);
			if(qlist.size()>0){
				model.addAttribute("questions",qlist);
			}
			clist = questionService.selectAllByuid(uId);
			if(clist.size()>0){
				model.addAttribute("collects",clist);
			}
		} catch (AppException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "userinfo";
	}
	//修改用户的基本信息
	@RequestMapping(value="/updateinfo/{uId}")
	public @ResponseBody String updateuserinfo( HttpServletRequest req,@PathVariable Integer uId,String uName,String uPhone){
		String str = "";
		HttpSession session = req.getSession();
		Users user = (Users) req.getSession().getAttribute("user");
		if("".equals(uName)){
			str = "1";
		}else if("".equals(uPhone)){
			str = "2";
		}else if("".equals(uName)&&"".equals(uPhone)){
			str="3";
		}else{
			try {
				userService.updateUserInfo(uId, uName, uPhone);
				str = "0";
			} catch (AppException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		return str ;
	}
	//修改用户的密码
	@RequestMapping(value="/updatepwd/{uId}")
	public @ResponseBody String updatepwd(HttpServletRequest req,@PathVariable Integer uId,String pwdcheck,String newpwd){
		String str = "";
		MD5 md5 = new MD5();
		HttpSession session = req.getSession();
		Users user = (Users) req.getSession().getAttribute("user");
		if(user!=null){
			if(user.getuPwd().equals(md5.DoMD5(pwdcheck))){
				try {
					userService.updatePwd(uId, md5.DoMD5(newpwd));
					str = "0";
				} catch (AppException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else{
				str = "1";
			}
		}
		return str;
	}
	
}
